﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RTUParameterConfig.Class;

namespace RTUParameterConfig.Controls
{
    public partial class WaterQuality : UserControl
    {
        public int intIndex;
        public ParameterConfig mf;

        public WaterQuality(ParameterConfig mfs)
        {
            InitializeComponent();
            mf = mfs;
        }

        /// <summary>
        /// 初始化参数
        /// </summary>
        /// <param name="type">设备类型</param>
        /// <param name="index">设备标识</param>
        public void InitValue(int type, int index)
        {
            gc.Text = Common.WaterQualityList[index].ToString();
            intIndex = index;
            this.Tag = type.ToString() + index.ToString();
            if (index == 3 || index == 4)
            {
                this.Tag = "53";
            }

            if (index == 8 || index == 9 || index == 10)
            {
                this.Tag = "58";
            }

            this.TabIndex = (type + 1) * 10 + index;

            if (index == 9)
            {
                labelControl54.Visible = false;
                nudAddr.Visible = false;
                nudAddr.Value = 255;
            }
        }

        /// <summary>
        /// 设置水质传感器配置
        /// </summary>
        public void SetValue()
        {
            if (intIndex == 0)
            {
                nudAddr.Value = ParameterConfig.rtuConfigInfo.WaterQuality_PH_Addr;
            }

            if (intIndex == 1)
            {
                nudAddr.Value = ParameterConfig.rtuConfigInfo.WaterQuality_Rjy_Addr;
            }

            if (intIndex == 2)
            {
                nudAddr.Value = ParameterConfig.rtuConfigInfo.WaterQuality_Ddl_Addr;
            }

            if (intIndex == 3)
            {
                nudAddr.Value = ParameterConfig.rtuConfigInfo.WaterQuality_Zd1000_Addr;
            }

            if (intIndex == 4)
            {
                nudAddr.Value = ParameterConfig.rtuConfigInfo.WaterQuality_Zd4000_Addr;
            }

            if (intIndex == 5)
            {
                nudAddr.Value = ParameterConfig.rtuConfigInfo.WaterQuality_COD_Addr;
            }

            if (intIndex == 6)
            {
                nudAddr.Value = ParameterConfig.rtuConfigInfo.WaterQuality_Ad_Addr;
            }

            if (intIndex == 7)
            {
                nudAddr.Value = ParameterConfig.rtuConfigInfo.WaterQuality_Yl_Addr;
            }

            if (intIndex == 8 || intIndex == 9 || intIndex == 10)
            {
                nudAddr.Value = ParameterConfig.rtuConfigInfo.WaterQuality_Sz_Addr;
            }

            if (intIndex == 11)
            {
                nudAddr.Value = ParameterConfig.rtuConfigInfo.WaterQuality_ORP_Addr;
            }
        }

        /// <summary>
        /// 获取水质传感器配置
        /// </summary>
        public void GetValue()
        {
            if (intIndex == 0)
            {
                ParameterConfig.rtuConfigInfo.WaterQuality_Enable[0] |= 0x0001 << 0;
                ParameterConfig.rtuConfigInfo.WaterQuality_PH_Addr = Convert.ToByte(nudAddr.Value);
            }

            if (intIndex == 1)
            {
                ParameterConfig.rtuConfigInfo.WaterQuality_Enable[0] |= 0x0001 << 1;
                ParameterConfig.rtuConfigInfo.WaterQuality_Rjy_Addr = Convert.ToByte(nudAddr.Value);
            }

            if (intIndex == 2)
            {
                ParameterConfig.rtuConfigInfo.WaterQuality_Enable[0] |= 0x0001 << 2;
                ParameterConfig.rtuConfigInfo.WaterQuality_Ddl_Addr = Convert.ToByte(nudAddr.Value);
            }

            if (intIndex == 3)
            {
                ParameterConfig.rtuConfigInfo.WaterQuality_Enable[0] |= 0x0001 << 3;
                ParameterConfig.rtuConfigInfo.WaterQuality_Zd1000_Addr = Convert.ToByte(nudAddr.Value);
            }

            if (intIndex == 4)
            {
                ParameterConfig.rtuConfigInfo.WaterQuality_Enable[0] |= 0x0001 << 4;
                ParameterConfig.rtuConfigInfo.WaterQuality_Zd4000_Addr = Convert.ToByte(nudAddr.Value);
            }

            if (intIndex == 5)
            {
                ParameterConfig.rtuConfigInfo.WaterQuality_Enable[0] |= 0x0001 << 5;
                ParameterConfig.rtuConfigInfo.WaterQuality_COD_Addr = Convert.ToByte(nudAddr.Value);
            }

            if (intIndex == 6)
            {
                ParameterConfig.rtuConfigInfo.WaterQuality_Enable[0] |= 0x0001 << 6;
                ParameterConfig.rtuConfigInfo.WaterQuality_Ad_Addr = Convert.ToByte(nudAddr.Value);
            }

            if (intIndex == 7)
            {
                ParameterConfig.rtuConfigInfo.WaterQuality_Enable[0] |= 0x0001 << 7;
                ParameterConfig.rtuConfigInfo.WaterQuality_Yl_Addr = Convert.ToByte(nudAddr.Value);
            }

            if (intIndex == 8)
            {
                ParameterConfig.rtuConfigInfo.WaterQuality_Enable[1] |= 0x0001 << 0;
                ParameterConfig.rtuConfigInfo.WaterQuality_Sz_Addr = Convert.ToByte(nudAddr.Value);
            }

            if (intIndex == 9)
            {
                ParameterConfig.rtuConfigInfo.WaterQuality_Enable[1] |= 0x0001 << 1;
                //ParameterConfig.rtuConfigInfo.WaterQuality_Sz_Addr = Convert.ToByte(nudAddr.Value);
            }

            if (intIndex == 10)
            {
                ParameterConfig.rtuConfigInfo.WaterQuality_Enable[1] |= 0x0001 << 2;
                ParameterConfig.rtuConfigInfo.WaterQuality_Sz_Addr = Convert.ToByte(nudAddr.Value);
            }

            if (intIndex == 11)
            {
                ParameterConfig.rtuConfigInfo.WaterQuality_Enable[1] |= 0x0001 << 3;
                ParameterConfig.rtuConfigInfo.WaterQuality_ORP_Addr = Convert.ToByte(nudAddr.Value);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除设备?", "删除设备", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                Control c = this.Parent;
                c.Controls.Remove(this);
                mf.listWaterTemperature.Remove((intIndex + 5).ToString());
                mf.BindWaterTemperature();
            }
        }

        private void btnDebug_Click(object sender, EventArgs e)
        {
            MessageInfo.SendMessage(ParameterConfig.mainFormName, ParameterConfig.debugMsg, 6, intIndex + 1);
        }
    }
}
